<template>
	<wsPage :title="$t('accountSettings.AccountSettings')">
		<div slot="page_operate">
		</div>
		<wsPanel>
			<template slot="panel_head_title">{{$t('accountSettings.AccountSettings')}}</template>
			<div slot="panel_body" class="profile-div">
				<div class="profile-form">
					<Form :label-width="100" ref="form1" :model="form1.model" :rules="form1.rules">
						<FormItem :label="$t('accountSettings.username')" required>
							<Input v-model="form1.model.userName" disabled></Input>
						</FormItem>
						<FormItem :label="$t('accountSettings.accountNumber')" required>
							<Input v-model="form1.model.loginName" disabled></Input>
						</FormItem>
						<FormItem :label="$t('accountSettings.oldPassword')" prop="password">
							<Input v-model="form1.model.password" type="password" autofocus></Input>
						</FormItem>
						<FormItem :label="$t('accountSettings.newPassword')" prop="newPassword">
							<Input v-model="form1.model.newPassword" type="password"></Input>
						</FormItem>
						<FormItem :label="$t('accountSettings.confirmPassword')" prop="confirmPassword">
							<Input v-model="form1.model.confirmPassword" type="password"></Input>
						</FormItem>
						<FormItem>
							<Button type="primary" @click="changePwd" long>{{$t('accountSettings.submit')}}</Button>
						</FormItem>
					</Form>
				</div>
			</div>
		</wsPanel>

	</wsPage>
</template>

<script>
	import Util from '../../../common/util.js';
	import Common from '../../../common/common.js';
	
	export default {
		data() {
			const validatePass = (rule, value, callback) => {
				if(value == this.form1.model.password) {
					callback(new Error('新密码不能和旧密码一样!'));
				} else {
					callback();
				}
			};
			const validatePassCheck = (rule, value, callback) => {
				if(value != this.form1.model.newPassword) {
					callback(new Error('两次输入密码不一致!'));
				} else {
					callback();
				}
			};
			const validateCheckPassword = (rule, value, callback) =>{
				if(value != this.updateModal.form.password) {
					callback(new Error('两次输入密码不一致!'));
				} else {
					callback();
				}
			};
			return {
            	flag: false,
	            clock: '',
	            nums: 60,
	            btn: null,
				form1: {
					model: {
						userName: '',
						loginName:'',
						password: '',
						newPassword: '',
						confirmPassword: ''
					},
					rules: {
						password: [
							{ required: true, message: '请输入原密码'},
							{ min: 6, message: '密码长度不符合规范'}
						],
						newPassword: [
							{ required: true, message: '请输入新密码'},
							{ validator: Common.validateLoginPassword, trigger: 'blur' },
							{ validator: validatePass}
						],
						confirmPassword: [
							{ required: true, message: '请输入确认密码'},
							{ validator: Common.validateLoginPassword, trigger: 'blur' },
							{ validator: validatePassCheck}
						]
					}
				}
			}
		},
		computed: {
			
		},
		mounted() {
			this.init();
			// 当前选择的语言版本
			let getCurrentLanguage=localStorage.getItem('currentLanguage');
			console.log(getCurrentLanguage);
			// console.log(this.$i18n.messages);
			this.$i18n.locale=getCurrentLanguage;
			if(this.$i18n.locale=='en'){
				this.form1.rules={
					password: [
						{required: true,message: 'Please enter the original password'},
						{ min: 6, message: 'Password length does not meet specifications'}
					],
					newPassword: [
						{ required: true, message: 'Please enter a new password'},
						{ validator: Common.validateLoginPassword, trigger: 'blur' },
					],
					confirmPassword: [
						{ required: true, message: 'Please enter a confirmation password'},
						{ validator: Common.validateLoginPassword, trigger: 'blur' },
					]
				};
			}else if(this.$i18n.locale=='it'){
				this.form1.rules={
					password: [
						{required: true,message: 'Si prega di inserire la password originale'},
						{ min: 6, message: 'La lunghezza della password non corrisponde alle specifiche'}
					],
					newPassword: [
						{ required: true, message: 'Si prega di inserire una nuova password'},
						{ validator: Common.validateLoginPassword, trigger: 'blur' },
					],
					confirmPassword: [
						{ required: true, message: 'Si prega di inserire una password di conferma'},
						{ validator: Common.validateLoginPassword, trigger: 'blur' },
					]
				};
			}

			

		},
		methods: {
			/**
			 * 初始化
			 */
			init() {
				let username_ = this.$store.getters.auth_username;
				let loginname_ = this.$store.getters.auth_loginName;
				this.form1.model = {
					userName: username_,
					loginName: loginname_,
					password: '',
					newPassword: '',
					confirmPassword: ''
				};
				this.$refs['form1'].resetFields();
			},
			/**
			 * 修改密码
			 */
			changePwd() {
				this.$refs['form1'].validate((valid) => {
					if (valid) {
                        this.sumitChangePwd();
                    } 
                    
               });				
			},
			/**
			 * 提交修改密码
			 */
			sumitChangePwd() {
				let _this = this;
				let postData_ = {
					password: this.form1.model.newPassword,
					oldpwd: this.form1.model.password
				};
				this.axios.put('/ws/sys/changepwd', postData_).then(function(response) {
					let res = response.data;
					if(0 === res.meta.code) {
//						_this.$Message.success('修改密码成功,请重新登录');
//						_this.init();
						if(_this.$i18n.locale=='zh'){
							_this.$Notice.info({
								title: '操作成功',
								desc: '密码修改成功，你需要退出重新登录',
								duration: 2,
								onClose: function() {
									window.location.href = 'login.html';
								}
							});
						}else if(_this.$i18n.locale=='it'){
							_this.$Notice.info({
								title: 'Operazione riuscita',
								desc: 'La password è stata modificata correttamente, è necessario uscire dal re-login',
								duration: 2,
								onClose: function() {
									window.location.href = 'login.html';
								}
							});
						}else if(_this.$i18n.locale=='en'){
							_this.$Notice.info({
								title: 'Successful operation',
								desc: 'The password is modified successfully, you need to quit re-login',
								duration: 2,
								onClose: function() {
									window.location.href = 'login.html';
								}
							});
						}						

					} else {
						let message_ = res.meta.message;
						if(_this.$i18n.locale=='zh'){
							_this.$Message.warning('修改密码失败:' + message_);
						}else if(_this.$i18n.locale=='en'){
							_this.$Message.warning('Password change failed:' + message_);
						}else if(_this.$i18n.locale=='it'){
							_this.$Message.warning('Cambio password fallito:' + message_);
						}
						
					}
				});	
			},

		}
	}
</script>

<style scoped="scoped">
	.btnDisabled {
	    cursor: not-allowed!important;
	}
	.profile-div {
		display: flex;
		justify-content: center;
		align-items: center;
		padding: 45px 0px 45px 0;
	}
	.profile-form {
		width: 550px;
	}
</style>